package com.example.mi.myjavamall.ui;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.example.baselibrary.common.AppManager;
import com.example.baselibrary.rx.RxBus;
import com.example.baselibrary.ui.activity.BaseActivity;
import com.example.baselibrary.utils.ToastUtil;
import com.example.baselibrary.widgets.BottomNavBar;
import com.example.mi.myjavamall.R;
import com.example.mi.myjavamall.ui.fragment.HomeFragment;
import com.example.mi.myjavamall.ui.fragment.MeFragment;

import java.util.ArrayList;
import java.util.Stack;

import butterknife.BindView;

/**
 * 主界面
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.mBottomNavBar)
    BottomNavBar mBottomNavBar;

    private Long pressTime = 0l;
    private Stack<Fragment> mStack;          //Fragment 栈管理
    private Fragment currentFragmnet;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initFragment();
        initBottomNav();
        initObserve();
    }

    //初始化Fragment栈管理
    private void initFragment() {
        mStack = new Stack<Fragment>();
        mStack.add(HomeFragment.getInstance());         //主界面Fragment
        mStack.add(HomeFragment.getInstance());
        mStack.add(HomeFragment.getInstance());
        mStack.add(HomeFragment.getInstance());
        mStack.add(MeFragment.getInstance());           //"我的"Fragment

        changeFragment(0);
    }

    //初始化底部导航切换事件
    private void initBottomNav() {
        mBottomNavBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                changeFragment(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
        mBottomNavBar.checkMsgBadge(false);
    }

    //切换Tab，切换对应的Fragment
    private void changeFragment(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (null != currentFragmnet) {
            transaction.hide(currentFragmnet);
        }
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(mStack.get(position).getClass().getName());
        if (fragment == null) {
            fragment = mStack.get(position);
        }
        currentFragmnet = fragment;

        if (!fragment.isAdded()) {
            transaction.add(R.id.mContainer, fragment, fragment.getClass().getName());
        } else {
            transaction.show(fragment);
        }
        transaction.commitAllowingStateLoss();
    }

    //初始化监听，购物车数量变化及消息标签是否显示
    private void initObserve() {

    }

    /**
     * 重写Back事件，双击退出
     */
    @Override
    public void onBackPressed() {
        Long time = System.currentTimeMillis();
        if (time - pressTime > 2000) {
            ToastUtil.ToastMsg(MainActivity.this, "再按一次退出程序");
            pressTime = time;
        } else {
            AppManager.getInstance().appExit();
        }
    }


}
